package com.csun.nusing.presenter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.csun.nusing.presenter.inter.IBase;
import com.linktop.whealthService.HealthApi;
import com.linktop.whealthService.HealthApi.BleCallBack;
import com.linktop.whealthService.HealthApi.EcgCallBack;
import com.linktop.whealthService.HealthApi.HealthApiCallBack;
import com.linktop.whealthService.OnBLEService;
import com.linktop.whealthService.task.BT_task;
import com.linktop.whealthService.task.Bp_task;
import com.linktop.whealthService.task.Bs_task;
import com.linktop.whealthService.task.Ox_task;
import com.linktop.whealthService.task.battery_task;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 随访仪设备主界面 ClassName: SuiFangDevicePresenter
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-24
 */
public class SuiFangDevicePresenter extends BasePresenter<IBase> {

	private HealthApi mHealthApi;
	private Timer batteryQueryTimer;
	
	
	public SuiFangDevicePresenter(IBase view) {
		super(view);
	}

	/**
	 * 停止计时器
	 * @Description: TODO
	 * @param    
	 * @return void  
	 * @throws
	 * @author fzq
	 * @date 2017-10-25
	 */
	public void stopTime(){
		if (batteryQueryTimer!=null) {
			batteryQueryTimer.cancel();
			batteryQueryTimer = null;
		}
	}
	
	
	
	/**
	 * sdk的初始话
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-11
	 */
	public HealthApi initHealth(Context context) {
		mHealthApi = new HealthApi();
		mHealthApi.init(context, new healthapicallback(),
				mbpHandler, mbtHandler, moxHandler, mbsHandler,
				new ecgcallback());
		mHealthApi.setBleCallBack(new blecallback());
		Log.e("version", HealthApi.getVersion());
		return mHealthApi;
	}
	
	
	private class healthapicallback implements HealthApiCallBack{

		@Override
		public void healthCallBack(int type, Object data) {
			// TODO Auto-generated method stub
			switch (type) {
			case HealthApi.BIND_FINISHED:
				break;
			case HealthApi.BATTERY:
				setBatteryUi((byte[]) data);
				break;
			case HealthApi.UPDATA_CON_INTERVAL_FAIL_ACTION:
				break;
			case HealthApi.QUERY_ECG_MODULE_EXIST:
				byte modual = (Byte) data;
				break;
			case HealthApi.QUERY_BS_MODULE_EXIST:
				byte modual1 = (Byte) data;
				break;
			case HealthApi.INTENT_DEVICE_VERSION_SOFE:
				String sofe = (String) data;
				break;
			case HealthApi.INTENT_DEVICE_VERSION_HARD:
				String hard = (String) data;
				break;
			}
		}
	}
	
	
	private class blecallback implements BleCallBack{

		@Override
		public void bleCallBack(int type, Object data) {
			if (type == HealthApi.BLE_STATUS) {
				setBleUiState((Integer) data);
			}
		}
	}
	
	
	private void modual_init() {
		mHealthApi.ecgQuery();
		mHealthApi.bsQuery();
		mHealthApi.batteryQuery();

		if (batteryQueryTimer == null)
			batteryQueryTimer = new Timer();
			batteryQueryTimer.schedule(new TimerTask() {
			@Override
			public void run() {
				mHealthApi.batteryQuery();
			}
		}, 300000, 300000);
	}

	private void setBleUiState(int mState) {

		switch (mState) {
		case OnBLEService.BLE_CONNECTED:
			break;
		case OnBLEService.BLE_DISCONNECTED:
			break;
		case OnBLEService.BLE_NOTIFICATION_ENABLED:
			modual_init();
			break;
		case OnBLEService.BLE_NOTIFICATION_NOTENABLED:
			break;
		default:
			break;
		}

	}

	private void setBatteryUi(byte[] battery) {
		switch (battery[0]) {
		case battery_task.BATTERY_QUERY:
			break;
		case battery_task.BATTERY_CHARING:
			break;
		case battery_task.BATTERY_FULL:
		default:
			break;
		}
	}
	
	
	
	private class  ecgcallback implements EcgCallBack{

		@Override
		public void onReceive(int[] data) {
			// TODO Auto-generated method stub
			switch (data[0]) {
			case HealthApi.ECG_DATA:
				// tv.append("SmoothedRaw: " + data + "\n");
				break;
			case HealthApi.ECG_HEARTRATE:
				final int avg_hr = data[1];
				break;
			case HealthApi.ECG_RRMAX:
				final int rrmaxint = data[1];
				break;
			case HealthApi.ECG_RRMIN:
				final int rrminint = data[1];
				break;
			case HealthApi.ECG_HRV:
				final int hRV = data[1];
				break;
			case HealthApi.ECG_MOOD:
				final int mood = data[1];
				break;
			case HealthApi.ECG_BR:
				// tv.append("Robust HR: " + data + "\n");
				Log.i("Robust HR ", data + "\n" + "");
				final int br = data[1];
				break;
			case HealthApi.ECG_END:
				break;
			default:
				break;
			}
		}
	}
	
	/**
	 * 温度测量
	 */
	private Handler mbtHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case BT_task.BT_RESULT:
				double result = msg.getData().getDouble("BodyTempResult");
				break;
			default:
				break;
			}
		}
	};

	@SuppressLint("HandlerLeak")
	private Handler moxHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Ox_task.OXYGEN_TEST_FINISH:
				double[] data = msg.getData().getDoubleArray("oxyResult");
				if (data != null) {
				}
				break;
			case Ox_task.AXIS_DATA:
				int red_value = msg.getData().getInt("oxDataRed");

				break;

			case Ox_task.OX_CMD_TIMEOUT:
				int ox_cmd_type = msg.getData().getInt("ox_cmd_type");
				break;
			default:
				break;
			}
		}
	};

	@SuppressLint("HandlerLeak")
	private Handler mbpHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Bp_task.BP_RESULT:
				int systolic = msg.arg1;
				int diastolic = msg.arg2;
				int heartRate = msg.getData().getInt("heartRate");
				break;
			case Bp_task.LOUQI:
				break;

			default:
				break;
			}
		}
	};
	@SuppressLint("HandlerLeak")
	private Handler mbsHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Bs_task.BS_PAPER_STATUS:
				int init_paper = msg.getData().getByte("initpaperStatus");
				break;
			case Bs_task.BS_PAPER_IS_READY:
				break;
			case Bs_task.BS_PAPER_CHECK_TIMEOUT:
				break;
			case Bs_task.BS_BLOOD_CHECK_TIMEOUT:
				break;
			case Bs_task.BS_TESTING_PAPER_OUT:
				break;
			case Bs_task.BS_RESULT:
				break;
			case Bs_task.BS_PAPER_IS_USED:
				break;
			case Bs_task.BS_GET_VER_FAIL:
				break;
			case Bs_task.BS_BLOOD_IN_DETECTED:
				break;
			default:
				break;
			}
		}
	};

}